package ms.unicorn.language.languagedef;

import java.util.List;

import org.eclipse.draw2d.geometry.Dimension;
import org.eclipse.draw2d.geometry.Insets;
import org.eclipse.draw2d.geometry.Rectangle;

public class ParameterUtil {
	
/*	public static final String DOCK = "dock";
	public static final String ANCHOR = "anchor";
	public static final String VISIBLE = "visible";
	public static final String POSITION = "position";
	public static final String SIZE = "size";
	public static final String EDITABLE = "editable";
	public static final String RESIZABLE = "resizable";
	public static final String ENABLED = "enable";
	public static final String MARGIN = "margin";
	public static final String LOCATION = "location";*/
	
	public static int indexOf(List<Parameter> list, ParameterType paramId) {
		for(int i = 0; i < list.size(); i ++)
			if (list.get(i).getParamId() == paramId)
				return i;
		return -1;
	}
	
	public static boolean hasParameter(List<Parameter> list, ParameterType paramId) {
		return indexOf(list, paramId) >= 0;
	}
	
	public static Parameter getParameter(List<Parameter> list, ParameterType paramId) {
		int idx = indexOf(list, paramId);
		return idx >= 0 ? list.get(idx) : null;
	}
	
	public static DockType getDock(List<Parameter> list, DockType defaultDock) {
		return getParameter(list, ParameterType.DOCK, defaultDock);
	}
	
	public static Rectangle getPosition(List<Parameter> list, Rectangle defaultLocation) {
		return getParameter(list, ParameterType.POSITION, defaultLocation);
	}
	
	public static Dimension getSize(List<Parameter> list, Dimension defaultSize) {
		return getParameter(list, ParameterType.SIZE, defaultSize);
	}
	
	@SuppressWarnings("unchecked")
	public static <T> T getParameter(List<Parameter> list, ParameterType paramId, T defaultValue) {
		XRectangle r;
		T result = defaultValue;
		Parameter param = getParameter(list, paramId);
		if (param != null)
			switch(param.getParamId()) {
			case ANCHOR:
				result = (T) param.getAnchorValue();
				break;
			case POSITION:
			case BOUND:
				r = param.getBoundValue();
				result = (T) new Rectangle(XbaseUtil.evaluate(r.getX(),0), XbaseUtil.evaluate(r.getY(),0), 
								XbaseUtil.evaluate(r.getW(),0), XbaseUtil.evaluate(r.getH(),0));
				break;
			case MARGIN:
				r = param.getBoundValue();
				result = (T) new Rectangle(XbaseUtil.evaluate(r.getY(),0), XbaseUtil.evaluate(r.getX(),0), 
								XbaseUtil.evaluate(r.getH(),0), XbaseUtil.evaluate(r.getW(),0));
				break;
			case SIZE:
				r = param.getBoundValue();
				result = (T) new Dimension(XbaseUtil.evaluate(r.getX(),0), XbaseUtil.evaluate(r.getY(),0));
				break;
			case DOCK:
				result = (T) param.getDockValue();
				break;
			case CATEGORY:
			case DESCRIPTION:
			case EDITABLE:
			case MAX:
			case MIN:
			case MULTILINE:
			case TEXT:
			case NOTIFY:
			case PRIMARY:
			case RESIZABLE:
			case VISIBLE:
				result = (T)XbaseUtil.evaluate(param.getValue(), defaultValue);
				break;
			case LOCATION:
				result = (T) param.getLocationValue();
				break;
			case LOCATOR:
				result = LanguageDefUtil.resolveDefinition(param.getItem());
				break;
			
			}
		
		return result;
	}
	
	public static Insets getMargin(List<Parameter> list, Insets defaultInsets) {
		return getParameter(list, ParameterType.MARGIN, defaultInsets);
	}
	
	public static boolean getVisible(List<Parameter> list, boolean defaultVis) {
		return getParameter(list, ParameterType.VISIBLE, defaultVis);
	}
	
	public static boolean getEditable(List<Parameter> list, boolean defaultEditable) {
		return getParameter(list, ParameterType.EDITABLE, defaultEditable);
	}
	
	public static DecoratorLocation getConnectorLocation(List<Parameter> list, DecoratorLocation defaultLocation) {
		return getParameter(list, ParameterType.LOCATION, defaultLocation);
	}
	
	public static DecoratorLocatorDef getConnectorLocator(List<Parameter> list) {
		return getParameter(list, ParameterType.LOCATOR, null);
	}
}
